package com.example.xinhuazidian

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.View.OnClickListener
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.xinhuazidian.adapter.XHYAdapter
import com.example.xinhuazidian.bean.Xiehouyu
import com.example.xinhuazidian.databinding.ActivityXhyactivityBinding
import com.example.xinhuazidian.utils.JsonFileUtil
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.gyf.immersionbar.ImmersionBar
import com.yechaoa.yutilskt.YUtils
import java.lang.reflect.Type

/**
 * 歇后语
 */
class XHYActivity : AppCompatActivity(), OnClickListener {

    private lateinit var binding: ActivityXhyactivityBinding
    private lateinit var mapBeanList: MutableList<Xiehouyu>
    private var xhyAdapter: XHYAdapter? = null
    private var isWord = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityXhyactivityBinding.inflate(layoutInflater)
        setContentView(binding.root)

        load()
        initView()
        initData()
    }

    private fun initView() {
        //沉浸状态栏
        ImmersionBar.with(this)
            .statusBarDarkFont(true)//状态栏字体是深色，不写默认为亮色
            .init()

        binding.btnSs.setOnClickListener(this)
    }

    private fun initData() {

    }

    @SuppressLint("SetTextI18n")
    private fun load() {
        val str = JsonFileUtil.loadFile(this, "xiehouyu.json")

        val mapBeanListType: Type = object : TypeToken<ArrayList<Xiehouyu?>?>() {}.type
        mapBeanList = Gson().fromJson(str, mapBeanListType)

        binding.tvNum.text = "一共${mapBeanList.size}个成语"
        initAdapter(mapBeanList)
    }

    private fun initAdapter(list: MutableList<Xiehouyu>) {
        //设置布局排列方式，默认垂直排列
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        binding.rv.layoutManager = linearLayoutManager

        //加载适配器
        xhyAdapter = XHYAdapter(list)
        binding.rv.adapter = xhyAdapter
    }

    @SuppressLint("SetTextI18n")
    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.btn_ss -> {
                YUtils.showLoading(this, "加载中...")

                isWord = false

                for (i in mapBeanList.indices) {
                    if (mapBeanList[i].riddle.toString()
                            .contains(binding.et.text.toString().trim())
                    ) {
                        isWord = true
                    }
                }

                when {
                    TextUtils.isEmpty(binding.et.text.toString().trim()) -> {
                        load()
                    }
                    isWord -> {
                        val listOfList = mutableListOf<Xiehouyu>()

                        for (i in mapBeanList.indices) {
                            if (mapBeanList[i].riddle.toString()
                                    .contains(binding.et.text.toString().trim())
                            ) {
                                listOfList.add(mapBeanList[i])
                            }
                        }

                        binding.tvNum.text = "查到${listOfList.size}条歇后语"
                        initAdapter(listOfList)
                        YUtils.hideLoading()
                    }
                    else -> {
                        Toast.makeText(this, "没有查到包含这个字的歇后语哦", Toast.LENGTH_SHORT).show()
                        YUtils.hideLoading()
                    }
                }
            }
        }
    }
}